package com.software.levelupandroid.systemTask.model;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.software.levelupandroid.entity.Badge;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.home.CallBack;

import java.util.List;

/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月29日 11:31:16
 */
public class SystemShowViewModel extends ViewModel {

    private final MutableLiveData<Resource<List<SystemTask>>> systemTaskListLiveData;
    private final MutableLiveData<Resource<Badge>> badgeLiveData;


    public SystemShowViewModel(){
        systemTaskListLiveData = new MutableLiveData<>();
        badgeLiveData = new MutableLiveData<>();
    }

    public LiveData<Resource<List<SystemTask>>> getSystemTaskListLiveData() {
        return systemTaskListLiveData;
    }

    public MutableLiveData<Resource<Badge>> getBadgeLiveData() {
        return badgeLiveData;
    }

    /**根据标签显示任务*/
    public void getSystemTaskListByType(Context context, int type){
        SystemShowRepository.getSystemShowRepository().getAllTasksByStatus(new CallBack<List<SystemTask>>() {
            @Override
            public void onSuccess(List<SystemTask> data) {
                runOnMainThread(systemTaskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("home:", msg);
                runOnMainThread(systemTaskListLiveData, Resource.failure(msg, null));
            }
        }, context, type);
    }
    /**获取所有领取的系统任务*/
    public void getAllSystemTask(Context context){
        SystemShowRepository.getSystemShowRepository().getAllTasks(new CallBack<List<SystemTask>>() {
            @Override
            public void onSuccess(List<SystemTask> data) {
                runOnMainThread(systemTaskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("home:", msg);
                runOnMainThread(systemTaskListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    public void isFinishSystemTask(SystemTask systemTask, Context context){
        SystemShowRepository.getSystemShowRepository().isFinishSystemTask(new CallBack<Badge>() {
            @Override
            public void onSuccess(Badge data) {
                badgeRunOnMainThread(badgeLiveData, Resource.success(data));

                Resource<List<SystemTask>> resource = systemTaskListLiveData.getValue();
                List<SystemTask> systemTasks = resource.getData();
                if (systemTasks != null){
                    int index = systemTasks.indexOf(systemTask);
                    if (index >= 0){
                        systemTasks.set(index, systemTask);
                        resource.onSuccess(systemTasks);
                        runOnMainThread(systemTaskListLiveData, resource);
                    }else {
                        resource.onFailure("任务索引小于0", systemTasks);
                        runOnMainThread(systemTaskListLiveData, resource);
                    }
                }else {
                    resource.onFailure("未获取到任务", systemTasks);
                    runOnMainThread(systemTaskListLiveData, resource);
                }
            }

            @Override
            public void onFailure(String msg) {
                badgeRunOnMainThread(badgeLiveData, Resource.failure(msg, null));

                Resource<List<SystemTask>> resource = systemTaskListLiveData.getValue();
                List<SystemTask> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(systemTaskListLiveData, resource);
            }
        }, context, systemTask);
    }

    /**
     * 将LiveData的赋值放在主线程上
     * @param taskListLiveData
     * @param data
     */
    private void runOnMainThread(MutableLiveData<Resource<List<SystemTask>>> taskListLiveData, Resource<List<SystemTask>> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            taskListLiveData.setValue(data);
        else
            //当前线程不在主线程
            taskListLiveData.postValue(data);
    }

    private void badgeRunOnMainThread(MutableLiveData<Resource<Badge>> taskListLiveData, Resource<Badge> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            taskListLiveData.setValue(data);
        else
            //当前线程不在主线程
            taskListLiveData.postValue(data);
    }

}
